<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form :form="form" slot="detail">
                <a-row>
                    <a-col :span="24">
                        <a-form-item label="环保登记编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input
                                v-decorator="['registrationCode', validatorRules.registrationCode]"
                                placeholder="请输入环保登记编码"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="机械生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-date placeholder="请选择机械生产日期"
                                v-decorator="['producedTime', validatorRules.producedTime]"
                                :trigger-change="true" style="width: 100%" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="车牌号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['plateNo', validatorRules.plateNo]"
                                placeholder="请输入车牌号码"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="排放标准" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-dict-select-tag type="list"
                                v-decorator="['evnType', validatorRules.evnType]"
                                :trigger-change="true" dictCode="evn_type" placeholder="请选择排放标准" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="燃料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-dict-select-tag type="list"
                                v-decorator="['fuelType', validatorRules.fuelType]"
                                :trigger-change="true" dictCode="fuel_type" placeholder="请选择燃料类型" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="机械种类" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-dict-select-tag type="list"
                                v-decorator="['machineryType', validatorRules.machineryType]"
                                :trigger-change="true" dictCode="machinery_type"
                                placeholder="请选择机械种类" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="机械环保代码/产品识别码(PIN)" :labelCol="labelCol"
                            :wrapperCol="wrapperCol">
                            <a-input v-decorator="['pin', validatorRules.pin]"
                                placeholder="请输入机械环保代码/产品识别码(PIN)"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="机械型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input
                                v-decorator="['mechanicalModel', validatorRules.mechanicalModel]"
                                placeholder="请输入机械型号"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="发动机型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['engineModel', validatorRules.engineModel]"
                                placeholder="请输入发动机型号"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="发动机生产厂" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['engineProducer', validatorRules.engineProducer]"
                                placeholder="请输入发动机生产厂"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="发动机编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['engineNum', validatorRules.engineNum]"
                                placeholder="请输入发动机编号"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="整车（机）铭牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-image-upload isMultiple
                                v-decorator="['machineNameplate', validatorRules.machineNameplate]"></j-image-upload>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="发动机铭牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-image-upload isMultiple
                                v-decorator="['engineNameplate', validatorRules.engineNameplate]"></j-image-upload>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="机械环保信息标签" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <j-image-upload isMultiple
                                v-decorator="['machineLabel', validatorRules.machineLabel]"></j-image-upload>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="所属人（单位）" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['vehicleOwner', validatorRules.vehicleOwner]"
                                placeholder="请输入所属人（单位）"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                        <a-button @click="submitForm">提 交</a-button>
                    </a-col>
                </a-row>
            </a-form>
        </j-form-container>
    </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
    name: 'NoRoadUploadForm',
    components: {
    },
    props: {
        //流程表单data
        formData: {
            type: Object,
            default: () => { },
            required: false
        },
        //表单模式：true流程表单 false普通表单
        formBpm: {
            type: Boolean,
            default: false,
            required: false
        },
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data() {
        return {
            form: this.$form.createForm(this),
            model: {},
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            confirmLoading: false,
            validatorRules: {
                registrationCode: {
                    rules: [
                        { required: true, message: '请输入环保登记编码!' },
                    ]
                },
                producedTime: {
                    rules: [
                        { required: true, message: '请输入机械生产日期!' },
                    ]
                },
                plateNo: {
                    rules: [
                        { required: true, message: '请输入车牌号码!' },
                    ]
                },
                evnType: {
                    rules: [
                        { required: true, message: '请输入排放标准!' },
                    ]
                },
                fuelType: {
                    rules: [
                        { required: true, message: '请输入燃料类型!' },
                    ]
                },
                machineryType: {
                    rules: [
                        { required: true, message: '请输入机械种类!' },
                    ]
                },
                pin: {
                    rules: [
                        { required: true, message: '请输入机械环保代码/产品识别码(PIN)!' },
                    ]
                },
                mechanicalModel: {
                    rules: [
                        { required: true, message: '请输入机械型号!' },
                    ]
                },
                engineModel: {
                    rules: [
                        { required: true, message: '请输入发动机型号!' },
                    ]
                },
                engineProducer: {
                    rules: [
                        { required: true, message: '请输入发动机生产厂!' },
                    ]
                },
                engineNum: {
                    rules: [
                        { required: true, message: '请输入发动机编号!' },
                    ]
                },
                machineNameplate: {
                    rules: [
                        { required: true, message: '请输入整车（机）铭牌!' },
                    ]
                },
                engineNameplate: {
                    rules: [
                        { required: true, message: '请输入发动机铭牌!' },
                    ]
                },
                machineLabel: {
                    rules: [
                        { required: true, message: '请输入机械环保信息标签!' },
                    ]
                },
                vehicleOwner: {
                    rules: [
                        { required: true, message: '请输入所属人（单位）!' },
                    ]
                },
            },
            url: {
                add: "/noRoadUpload/noRoadUpload/add",
                edit: "/noRoadUpload/noRoadUpload/edit",
                queryById: "/noRoadUpload/noRoadUpload/queryById"
            }
        }
    },
    computed: {
        formDisabled() {
            if (this.formBpm === true) {
                if (this.formData.disabled === false) {
                    return false
                }
                return true
            }
            return this.disabled
        },
        showFlowSubmitButton() {
            if (this.formBpm === true) {
                if (this.formData.disabled === false) {
                    return true
                }
            }
            return false
        }
    },
    created() {
        //如果是流程中表单，则需要加载流程表单data
        this.showFlowData();
    },
    methods: {
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'registrationCode', 'producedTime', 'plateNo', 'evnType', 'fuelType', 'machineryType',
                    'pin', 'mechanicalModel', 'engineModel', 'engineProducer', 'engineNum', 'machineNameplate', 'engineNameplate', 'machineLabel',
                    'vehicleOwner'))
            })
        },
        //渲染流程表单数据
        showFlowData() {
            if (this.formBpm === true) {
                let params = { id: this.formData.dataId };
                getAction(this.url.queryById, params).then((res) => {
                    if (res.success) {
                        this.edit(res.result);
                    }
                });
            }
        },
        submitForm() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    let formData = Object.assign(this.model, values);
                    console.log("表单提交数据", formData)
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    })
                }

            })
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'registrationCode', 'producedTime', 'plateNo', 'evnType', 'fuelType', 'machineryType', 'pin',
                'mechanicalModel', 'engineModel', 'engineProducer', 'engineNum', 'machineNameplate', 'engineNameplate', 'machineLabel', 'vehicleOwner'))
        },
    }
}
</script>